package com.iplant.wms.serviceImpl.dao.wpo;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.excel.ExcelData;
import com.iplant.base.po.excel.ExcelLineData;
import com.iplant.base.po.excel.ExcelSheetData;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.cache.CacheUtils;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.crm.CRMCustomer; 
import com.iplant.basic.serviceImpl.dao.crm.CRMCustomerDAO;
import com.iplant.wms.service.basicenum.wpo.WMSPurchaseOrderStatus;
import com.iplant.wms.service.po.bill.WMSBillType;
import com.iplant.wms.service.po.wpo.WMSPurchaseOrder;
import com.iplant.wms.service.po.mfs.WMSStore;
import com.iplant.wms.serviceImpl.dao.bill.WMSBillTypeDAO;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@DBBaseDAO(WMSPurchaseOrder.class)
public class WMSPurchaseOrderDAO extends BaseDAO {


    private static WMSPurchaseOrderDAO Instance = null;

    private WMSPurchaseOrderDAO() {
        super("");
        this.Init();
    }

    public static WMSPurchaseOrderDAO getInstance() {
        if (Instance == null) {
            Instance = new WMSPurchaseOrderDAO();
        }
        return Instance;
    }






    /**
     * Excel数据解析
     */
    public List<WMSPurchaseOrder> GetWMSPurchaseOrderList(BMSEmployee wLoginUser, ExcelData wExcelData,
                                                          OutResult<Integer> wErrorCode) {
        List<WMSPurchaseOrder> wResult = new ArrayList<WMSPurchaseOrder>();
        wErrorCode.set(0);
        try {
            if (wExcelData.sheetData == null || wExcelData.sheetData.size() <= 0) {
                return wResult;
            }

            ExcelSheetData wSheetData = wExcelData.sheetData.get(0);
            if (wSheetData.lineData == null || wSheetData.lineData.size() <= 0) {
                return wResult;
            }


            int wRow = 0;

            List<WMSBillType> wOrderTypeList = WMSBillTypeDAO.getInstance().WMS_QueryBillTypeList(wLoginUser, 1,
                                                                                                     1,
                                                                                                     Pagination.getMaxSize(),
                                                                                                     wErrorCode);
            if (wErrorCode.Result != 0) {
                return wResult;
            }

            List<CRMCustomer> wCustomerList =CacheUtils.GetCacheTList(CRMCustomer.class);  CRMCustomerDAO.getInstance().CRM_QueryCustomerList(wLoginUser,"",
                                                                                                 "", "", "", "", "", -1,
                                                                                                 -1, 1,
                                                                                                 Pagination.getMaxSize(),
                                                                                                 wErrorCode);
            if (wErrorCode.Result != 0) {
                return wResult;
            }
            List<WMSStore> wStoreList = CacheUtils.GetCacheTList(WMSStore.class);
            if (wErrorCode.Result != 0) {
                return wResult;
            }
            Map<String, CRMCustomer> wCRMCustomerMap =
                    wCustomerList.stream().collect(Collectors.toMap(p -> p.Code, p -> p));
            Map<String, WMSStore> wWMSStoreMap = wStoreList.stream().collect(Collectors.toMap(p -> p.Code, p -> p));
            Map<String, WMSBillType> wWMSOrderTypeMap =
                    wOrderTypeList.stream().collect(Collectors.toMap(p -> p.Code, p -> p));


            for (ExcelLineData wExcelLineData : wSheetData.lineData) {
                if (wRow == 0) {
                    wRow++;
                    continue;
                }

                String wPONo = wExcelLineData.colData.get(0);
                String wType = wExcelLineData.colData.get(1);
                String wCustomer = wExcelLineData.colData.get(2);
                String wExTime1 = wExcelLineData.colData.get(3);
                String wExTime2 = wExcelLineData.colData.get(4);
                String wWBSNo = wExcelLineData.colData.get(5);
                String wSuperNo = wExcelLineData.colData.get(6);
                String wSuperName = wExcelLineData.colData.get(7);
                String wSuperAddress = wExcelLineData.colData.get(8);
                String wContactor = wExcelLineData.colData.get(9);
                String wPhone = wExcelLineData.colData.get(10);
                String wStore = wExcelLineData.colData.get(11);

                WMSPurchaseOrder wWMSPurchaseOrder = new WMSPurchaseOrder();
                wWMSPurchaseOrder.ID = 0;
                wWMSPurchaseOrder.Code = wPONo;
                wWMSPurchaseOrder.Active = WMSPurchaseOrderStatus.InProcurement.getValue();
                wWMSPurchaseOrder.CustomerCode = wCustomer;
                if (wCRMCustomerMap.containsKey(wWMSPurchaseOrder.CustomerCode)) {
                    wWMSPurchaseOrder.CustomerID = wCRMCustomerMap.get(wWMSPurchaseOrder.CustomerCode).ID;
                    wWMSPurchaseOrder.CustomerName = wCRMCustomerMap.get(wWMSPurchaseOrder.CustomerCode).Name;
                }
                if (wWMSOrderTypeMap.containsKey(wWMSPurchaseOrder.TypeCode)) {
                    wWMSPurchaseOrder.TypeID = wWMSOrderTypeMap.get(wWMSPurchaseOrder.TypeCode).ID;
                    wWMSPurchaseOrder.TypeName = wWMSOrderTypeMap.get(wWMSPurchaseOrder.TypeCode).Name;
                }
                //
                wWMSPurchaseOrder.ArriveStartTime = StringUtils.parseCalendar(wExTime1);
                wWMSPurchaseOrder.ArriveEndTime = StringUtils.parseCalendar(wExTime2);
                wWMSPurchaseOrder.POReference1 = wWBSNo;
                wWMSPurchaseOrder.SupplierCode = wSuperNo;
                wWMSPurchaseOrder.SupplierName = wSuperName;
                if (wCRMCustomerMap.containsKey(wWMSPurchaseOrder.SupplierCode)) {
                    wWMSPurchaseOrder.SupplierID = wCRMCustomerMap.get(wWMSPurchaseOrder.SupplierCode).ID;
                    if (StringUtils.isEmpty(wWMSPurchaseOrder.SupplierName)) {
                        wWMSPurchaseOrder.SupplierName = wCRMCustomerMap.get(wWMSPurchaseOrder.SupplierCode).Name;
                    }
                }
                wWMSPurchaseOrder.SupplierAddress = wSuperAddress;
                wWMSPurchaseOrder.SupplierContact = wContactor;
                wWMSPurchaseOrder.SupplierTel = wPhone;
                wWMSPurchaseOrder.StoreCode = wStore;
                if (wWMSStoreMap.containsKey(wWMSPurchaseOrder.StoreCode)) {
                    wWMSPurchaseOrder.StoreID = wWMSStoreMap.get(wWMSPurchaseOrder.StoreCode).ID;
                    wWMSPurchaseOrder.StoreName = wWMSStoreMap.get(wWMSPurchaseOrder.StoreCode).Name;

                }
                wResult.add(wWMSPurchaseOrder);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
        }
        return wResult;
    }




//    /**
//     * 物料明细包装打码
//     */
//    private void MaterialDetailsPackagingCoding(BMSEmployee wLoginUser, WMSPurchaseOrder wWMSPurchaseOrder,
//                                                List<WMSPurchaseOrderItem> wItemList) {
//        try {
//            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
//
//            for (WMSPurchaseOrderItem wWMSPurchaseOrderItem : wItemList) {
//                int wMinimumNumberOfPackages = wWMSPurchaseOrderItem.QtyReleased;
//                if (wMinimumNumberOfPackages <= 0) {
//                    wMinimumNumberOfPackages = 1;
//                }
//                double wFQTY = wWMSPurchaseOrderItem.OrderedQty;
//                int wTimes = 0;
//                if (wFQTY % wMinimumNumberOfPackages == 0) {
//                    wTimes = (int) wFQTY / wMinimumNumberOfPackages;
//                } else {
//                    wTimes = (int) wFQTY / wMinimumNumberOfPackages + 1;
//                }
//                for (int i = 1; i <= wTimes; i++) {
//                    double wRealN = wMinimumNumberOfPackages;
//                    if (i == wTimes && wFQTY % wMinimumNumberOfPackages != 0) {
//                        wRealN = i * wMinimumNumberOfPackages - wFQTY;
//                    }
//                    WMSPurchasePackagingCoding wWMSPurchasePackagingCoding = new WMSPurchasePackagingCoding(0,
//                            wWMSPurchaseOrder.ID, wWMSPurchaseOrderItem.ID, wWMSPurchaseOrderItem.SKU,
//                            Calendar.getInstance(), wLoginUser.ID, Calendar.getInstance(), wLoginUser.ID, 1, "", wRealN,
//                            wWMSPurchaseOrderItem.LotAttr01);
//                    WMSPurchasePackagingCodingDAO.getInstance().Update(wLoginUser, wWMSPurchasePackagingCoding,
//                            wErrorCode);
//                }
//            }
//        } catch (Exception ex) {
//            logger.error(ex.toString());
//        }
//    }
}
